package com.yantong.controller.order;

import com.yantong.annotation.Log;
import com.yantong.constant.RefundStatus;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.DateUtils;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.OrderRefund;
import com.yantong.service.RefundService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 退款申请
 *
 * @author kanghaixin
 * @date 2018/9/10.17:31
 */
@Controller
@RequestMapping("/order/refund")
public class RefundController extends BaseController {
    private static final String prefix = "order";

    @Autowired
    private RefundService refundService;

    /**
     * 退款申请页面
     *
     * @return
     */
    @RequiresPermissions("order:refund:refund")
    @GetMapping()
    public String refunds(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/refunds";
    }

    /**
     * 退款申请列表数据
     *
     * @param startcom.yantong.pojo.MarketingGroup
     * @param end
     * @param centreId
     * @param queryItem
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequiresPermissions("order:refund:refund")
    @GetMapping("/list")
    public PageUtils<OrderRefund> listOrderRefunds(String start, String end, Long centreId, String queryItem, int offset, int limit) {
        return refundService.listOrderRefund(DateUtils.parseDateForm(start), DateUtils.parseDateForm(end), centreId, getUserTeamId(), queryItem, 0, offset, limit);
    }

    /**
     * 退款审核
     *
     * @param refundId
     * @return
     */
    @Log(module = "订单退款管理", operation = "退款审核")
    @ResponseBody
    @RequiresPermissions("order:refund:check")
    @PostMapping("/refundCheck")
    public ResultVo refundCheck(Long refundId) {
        if (refundId == null) {
            return ResultVo.error();
        }
        OrderRefund refund = new OrderRefund();
        refund.setId(refundId);
        refund.setStatus(RefundStatus.checked.getCode());
        refund.setCheckmid(getUserId().intValue());
        refund.setCheckname(getNikeName());
        refund.setCheckTime(new Date());
        if (refundService.updateRefund(refund) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 批量审核
     *
     * @param ids
     * @return
     */
    @Log(module = "订单退款管理", operation = "批量审核")
    @ResponseBody
    @PostMapping("/batchCheck")
    @RequiresPermissions("order:refund:batchCheck")
    public ResultVo batchCheck(@RequestParam("ids[]") Long[] ids) {
        if (ids == null || ids.length < 1) {
            return ResultVo.error();
        }

        Date now = new Date();
        for (Long id : ids) {
            if (id == null) {
                continue;
            }
            OrderRefund refund = new OrderRefund();
            refund.setId(id);
            refund.setStatus(RefundStatus.checked.getCode());
            refund.setCheckmid(getUserId().intValue());
            refund.setCheckname(getNikeName());
            refund.setCheckTime(now);
            refundService.updateRefund(refund);
        }
        return ResultVo.ok();
    }

    /**
     * 退款关闭
     *
     * @param refundId
     * @return
     */
    @Log(module = "订单退款管理", operation = "退款关闭")
    @ResponseBody
    @PostMapping("/refundClose")
    @RequiresPermissions(value = {"order:refund:close", "finance:refund:close"}, logical = Logical.OR)
    public ResultVo refundClose(Long refundId) {
        if (refundId == null) {
            return ResultVo.error();
        }
        if (refundService.closeRefund(refundId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }
}
